import 'package:flutter/material.dart';

import '../item_detail.dart';
import '../model/recommend_card_bean.dart';

class RecommendCard extends StatefulWidget {
  const RecommendCard(this.cardData, {Key? key}) : super(key: key);
  final PItemCommonRecommendCard cardData;
  @override
  State<RecommendCard> createState() => _RecommendCardState();
}

class _RecommendCardState extends State<RecommendCard> {
  @override
  Widget build(BuildContext context) {
    return _buildRecommendCardView(context, widget.cardData);
  }

  Widget _buildRecommendCardView(BuildContext context, PItemCommonRecommendCard card) {
    final Size screenSize = MediaQuery.of(context).size;
    final double width = (screenSize.width - 8.0 - 8.0 - 8.0) / 2;
    return ClipRRect(
      borderRadius: const BorderRadius.all(Radius.circular(6.0)),
      child: GestureDetector(
        onTap: () {
          Navigator.push(context, MaterialPageRoute(builder: (context) {
            return const ItemDetail();
          }));
        },
        child: SizedBox(
          width: width,
          child: Stack(
            children: _buildCardWidgets(context, card, width),
          ),
        ),
      ),
    );
  }

  List<Widget> _buildCardWidgets(
      BuildContext context, PItemCommonRecommendCard card, double width) {
    return <Widget>[
      Column(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
        Stack(
          children: <Widget>[
            _buildMainImage(card, width),
            _buildMainImageMask(width),
          ],
        ),
        Container(
          padding: const EdgeInsets.fromLTRB(8.0, 9.0, 8.0, 4.0),
          width: width,
          color: Colors.white,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _buildTitle(card, width),
              _buildPriceInfo(card),
              _buildUserInfo(card)
            ],
          ),
        )
      ]),
    ];
  }

  Widget _buildUserInfo(PItemCommonRecommendCard card) {
    return Container(
      padding: const EdgeInsets.only(top: 7.0),
      child: Column(
        children: <Widget>[
          Container(
              width: double.infinity,
              height: 0.5,
              color: const Color(0xFFF0F0F0)),
          Container(
            width: double.infinity,
            height: 36.0,
            color: const Color(0xFFFFFFFF),
            child: Row(
              children: <Widget>[]
                ..add(
                    ClipRRect(
                      borderRadius: const BorderRadius.all(Radius.circular(3.0)),
                      child: Image.network(
                        card.avatarUrl,
                        width: 24.0,
                        height: 24.0,
                        fit: BoxFit.cover,
                      ),
                    )
                )
                ..add(
                    Expanded(
                      child: Container(
                        padding: const EdgeInsets.only(left: 4.0),
                        child: Text(
                          card.city,
                          maxLines: 1,
                          overflow: TextOverflow.clip,
                          style: const TextStyle(
                              fontSize: 10.0, color: Color(0xFF888888)),
                        ),
                      ),
                    )
                )
                ..add(
                  card.userTagUrl?.isNotEmpty == true ?
                    Container(
                      padding: const EdgeInsets.only(left: 4.0),
                      child: Align(
                        alignment: Alignment.centerRight,
                        child: Image.network(
                          card.userTagUrl!,
                          width: 80,
                          height: 16,
                          fit: BoxFit.fill,
                        ),
                      ),
                    ) : Container(),
                ),
            ),
          )
        ],
      ),
    );
  }

  Widget _buildPriceInfo(PItemCommonRecommendCard card) {
    return RichText(text: TextSpan(
      children: [
        TextSpan(
            text: '￥',
            style: TextStyle(
                color: Colors.red.shade400,
                fontSize: 10.0,
                fontWeight: FontWeight.bold)
        ),
        ..._buildPrice(card),
      ],),
    );
  }

  List<TextSpan> _buildPrice(PItemCommonRecommendCard card) {
    final List<String> priceList = card.price.split('.');
    final String part0 = priceList[0];
    final String? part1 = priceList.length > 1 && priceList[1] != '00'
        ? priceList[1] : null;

    return <TextSpan>[
      TextSpan(
          text: part0,
          style: TextStyle(
              color: Colors.red.shade400,
              fontSize: 16.0,
              fontWeight: FontWeight.bold)
      ),
      part1 != null
          ? TextSpan(
          text: '.$part1',
          style: TextStyle(
              color: Colors.red.shade400,
              fontSize: 10.0,
              fontWeight: FontWeight.bold)
      ) : const TextSpan(text: ''),
      card.want.isNotEmpty == true
          ? TextSpan(
          text: '  ${card.want}',
          style: const TextStyle(
              color: Color(0xFFA3A3A3),
              fontSize: 10.0,
              fontWeight: FontWeight.normal)
      ) : const TextSpan(text: ''),
    ];
  }

  Widget _buildMainImage(PItemCommonRecommendCard card, double width) {
    return ClipRRect(
      borderRadius: const BorderRadius.all(Radius.circular(6.0)),
      child: Image.network(
        card.mainPicUrl,
        width: width,
        height: width,
        fit: BoxFit.cover,
      ),
    );
  }

  Widget _buildMainImageMask(double size) {
    return Container(
      width: size,
      height: size,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: <Color>[Color(0x00000000), Color(0x07000000)],
        ),
      ),
    );
  }

  Widget _buildTitle(PItemCommonRecommendCard card, double maxWidth) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Container(
          margin: const EdgeInsets.only(right: 4.0),
          child: Image.network(
            card.tagUrl,
            width: 28,
            height: 16,
            fit: BoxFit.fill,
          ),
        ),
        SizedBox(
          width: maxWidth - 32 - 16,
          child: Text(
            card.title,
            style: const TextStyle(
                fontSize: 14.0, color: Color(0xFF222222)),
            maxLines: 1,
          ),
        ),
      ],
    );
  }
}
